/**
 * 方法拓展
 * 
 * 方法应适用于全局
 * 不应出现针对局部的数据
 * 不依赖于app或game
 */

require('Global');

cc.Global.getUrlParams = function () {
    var url = location.search;
    var ret = new Object();
    if (url.indexOf('?') != -1) {
        var str = url.substr(1);
        var strs = str.split('&');
        for (var i = 0; i < strs.length; i++) {
            ret[strs[i].split('=')[0]] = unescape(strs[i].split('=')[1]);
        }
    }
    return ret;
}

cc.Global.checkInput = function (type, text) {
    var checkFormat = function (judge, info) {
        if (!judge) {
            cc.Toast.message(info);
            return true;
        } else return false;
    }
    switch (type) {
        default: return checkFormat(text != '', 'Incorrect input!');
    }
}

cc.Global.formatCount = function (count) {
    var len = (count + '').length;
    var nowCount = 0;
    if (len <= 3) {
        nowCount = count;
    } else if (len >= 4 && len <= 6) {
        nowCount = Math.floor((count / 1000)) + 'K';
    } else if (len >= 7 && len <= 9) {
        nowCount = Math.floor((count / 1000000)) + 'M';
    } else if (len >= 10 && len <= 12) {
        nowCount = Math.floor((count / 1000000000)) + 'B';
    } else if (len >= 13 && len <= 15) {
        nowCount = Math.floor((count / 1000000000000)) + 'T';
    } else if (len >= 16 && len <= 18) {
        nowCount = Math.floor((count / 1000000000000000)) + 'AA';
    }
    return nowCount;
}

cc.Global.formatFlag = function (count) {
    var len = (count + '').length;
    var nowCount = 0;
    if (len <= 3) {
        nowCount = 0;
    } else if (len >= 4 && len <= 6) {
        nowCount = 1;
    } else if (len >= 7 && len <= 9) {
        nowCount = 2;
    } else if (len >= 10 && len <= 12) {
        nowCount = 3;
    } else if (len >= 13 && len <= 15) {
        nowCount = 4;
    } else if (len >= 16 && len <= 18) {
        nowCount = 5;
    }
    return nowCount;
}

cc.Global.formatNumberFlag = function (count) {
    var len = (count + '').length;
    var nowCount = 0;
    if (len <= 3) {
        nowCount = count;
    } else if (len >= 4 && len <= 6) {
        nowCount = Math.floor((count / 1000));
    } else if (len >= 7 && len <= 9) {
        nowCount = Math.floor((count / 1000000));
    } else if (len >= 10 && len <= 12) {
        nowCount = Math.floor((count / 1000000000));
    } else if (len >= 13 && len <= 15) {
        nowCount = Math.floor((count / 1000000000000));
    } else if (len >= 16 && len <= 18) {
        nowCount = Math.floor((count / 1000000000000000));
    }
    return nowCount;
}

cc.Global.formatNumber = function (count, signal) {
    var str = Math.abs(count) + '';
    var len = str.length;
    var ret = str;
    if (len > 3) {
        var r = len % 3;
        var t = str.slice(r, len).match(/\d{3}/g).join(',');
        ret = r > 0 ? (str.slice(0, r) + ',' + t) : t;
    }
    if (signal) ret = count < 0 ? ('-' + ret) : ('+' + ret);
    return ret;
}

cc.Node.prototype.find = function (path, componentType) {
    var node = cc.find(path, this);
    if (node && componentType) {
        return node.getComponent(componentType);
    }
    return node;
}

cc.Node.prototype.emitEvent = function (event, data) {
    var evt = new cc.Event.EventCustom(event, true);
    evt.detail = data;
    this.dispatchEvent(evt);
}

cc.Sprite.prototype.loadImage = function (url, cbk) {
    this.imageUrl = url;
    cc.loader.load(url, (error, texture) => {
        if (error) {
            cc.log('load image error: ', error);
            cbk && cbk(null);
            return;
        }
        if (url != this.imageUrl) return;
        this.spriteFrame = null;
        this.spriteFrame = new cc.SpriteFrame(texture);
        cbk && cbk(texture);
    });
}

cc.loadRes = function (path, cbk) {
    if (!path) return;
    cc.loader.loadRes('res-' + CONFIG.language + '/' + path, (err, res) => {
        if (err) {
            cc.loader.loadRes(path, (err2, res2) => {
                if (err2) {
                    cc.log('load res failure!', path);
                } else {
                    cbk && cbk(res2);
                }
            });
        } else {
            cbk && cbk(res);
        }
    });
}

String.prototype.format = function (args) {
    var result = this;
    if (arguments.length > 0) {
        if (arguments.length == 1 && typeof (args) == "object") {
            for (var key in args) {
                if (args[key] != undefined) {
                    var reg = new RegExp("({" + key + "})", "g");
                    result = result.replace(reg, args[key]);
                }
            }
        }
        else {
            for (var i = 0; i < arguments.length; i++) {
                if (arguments[i] != undefined) {
                    var reg = new RegExp("({)" + i + "(})", "g");
                    result = result.replace(reg, arguments[i]);
                }
            }
        }
    }
    return result;
}

cc.Emitter = (function () {

    var eventMap = {};

    return {

        on(event, callback, target) {
            !eventMap[event] && (eventMap[event] = []);
            eventMap[event].push({ callback: callback, target: target });
        },

        off(event, callback, target) {
            var array = eventMap[event];
            if (!array) return;
            for (var i = array.length - 1; i >= 0; i--) {
                var element = array[i];
                if (element && element.callback === callback) {
                    array.splice(i, 1);
                    break;
                }
            }
        },

        emit(event, data) {
            var array = eventMap[event];
            if (!array) return;
            for (var i = 0; i < array.length; i++) {
                var element = array[i];
                element && element.callback.call(element.target, data);
            }
        },
    }

})();

